#include <iostream>    
#include <fstream>    
#include <vector>    
#include <string>    
#include <limits>    
    
using namespace std;   

struct Wheel {
    string type;
    int size;
};

struct Chassis {
    string id;
    string model;
    int wheelbase;
    int trackwidth;
    int minGroundClearance;
    int minTurningRadius;
    string driveType;
    int maxTravelDistance;
    Wheel wheels[4];
};

struct AGXKit {
    string model;
    int aiPerformance;
    int cudaCores;
    int tensorCores;
    int vram;
    int storage;
};

struct StereoCamera {
    string model;
    string cameraType;
    pair<int, int> resolution;
    int frameRateRGB;
    int fov;
    int depthFrameRate;
};

struct Lidar {
    string model;
    int channelCount;
    int testRange;
    int powerConsumption;
};

struct Gyroscope {
    string model;
    string manufacturer;
};

struct LCD {
    float size;
    string model;
};

struct BatteryModule {
    string parameters;
    string externalPowerSupply;
    int chargingTime;
};

struct SmartCar {
    string id;
    Chassis chassis;
    AGXKit agxKit;
    StereoCamera stereoCamera;
    Lidar lidar;
    Gyroscope gyroscope;
    LCD lcd;
    BatteryModule batteryModule;
};

struct Student {
    string studentId;
    string name;
};

void saveToFile(const vector<SmartCar>& cars, const vector<Student>& students);
void loadFromFile(vector<SmartCar>& cars, vector<Student>& students);
void displayCarInfo(const SmartCar& car, const Student& student);
int main() {
    vector<SmartCar> cars(10); 
    vector<Student> students; 

     for (int i = 0; i < 10; ++i) {
        cars[i].id = "cqusn" + to_string(i + 1);
        cars[i].chassis.id = "dp" + to_string(i + 1);
        cars[i].chassis.model = "SCOUT MINI";
        cars[i].chassis.wheelbase = 451;
        cars[i].chassis.trackwidth = 490;
        cars[i].chassis.minGroundClearance = 115;
        cars[i].chassis.minTurningRadius = 0;
        cars[i].chassis.driveType = "四轮四驱";
        cars[i].chassis.maxTravelDistance = 10000;
        cars[i].chassis.wheels[0] = {"公路轮", 175};
        cars[i].chassis.wheels[1] = {"公路轮", 175};
        cars[i].chassis.wheels[2] = {"公路轮", 175};
        cars[i].chassis.wheels[3] = {"公路轮", 175};
        cars[i].agxKit.model = "AGX Xavier";
        cars[i].agxKit.aiPerformance = 32;
        cars[i].agxKit.cudaCores = 512;
        cars[i].agxKit.tensorCores = 64;
        cars[i].agxKit.vram = 32;
        cars[i].agxKit.storage = 32;
        cars[i].stereoCamera.model = "RealSense D435i";
        cars[i].stereoCamera.cameraType = "D430";
        cars[i].stereoCamera.resolution = {1920, 1080};
        cars[i].stereoCamera.frameRateRGB = 30;
        cars[i].stereoCamera.fov = 8758;
        cars[i].stereoCamera.depthFrameRate = 90;
        cars[i].lidar.model = "RS-Helios-16p";
        cars[i].lidar.channelCount = 16;
        cars[i].lidar.testRange = 100;
        cars[i].lidar.powerConsumption = 8;
        cars[i].gyroscope.model = "CH110";
        cars[i].gyroscope.manufacturer = "NXP";
        cars[i].lcd.size = 11.6;
        cars[i].lcd.model = "super";
        cars[i].batteryModule.parameters = "24V/15Ah";
        cars[i].batteryModule.externalPowerSupply = "24V";
        cars[i].batteryModule.chargingTime = 2;
    }

    students.push_back({"s001", "zhao"});
    students.push_back({"s002", "qian"});
    students.push_back({"s003", "sun"});
    students.push_back({"s004", "li"});
    students.push_back({"s005", "zhou"});
    students.push_back({"s006", "wu"});
    students.push_back({"s007", "֣zheng"});
    students.push_back({"s008", "wang"});
    students.push_back({"s009", "feng"});
    students.push_back({"s010", "chen"});

    saveToFile(cars, students);

    loadFromFile(cars, students);

    int currentIndex = 0;
    while (true) {
        displayCarInfo(cars[currentIndex], students[currentIndex]);
        cout << "Press 'n' for next, 'p' for previous or 'q' to quit: ";
        char ch;
        cin >> ch;
        if (ch == 'n') {
            currentIndex = (currentIndex + 1) % cars.size();
        } else if (ch == 'p') {
            currentIndex = (currentIndex - 1 + cars.size()) % cars.size();
        } else if (ch == 'q') {
            break;
        }
    }

    return 0;
}

void saveToFile(const vector<SmartCar>& cars, const vector<Student>& students) {
    ofstream file("smartcars.dat", ios::binary);
    file.write(reinterpret_cast<const char*>(&cars), sizeof(cars));
    file.write(reinterpret_cast<const char*>(&students), sizeof(students));
    file.close();
}

void loadFromFile(vector<SmartCar>& cars, vector<Student>& students) {
    ifstream file("smartcars.dat", ios::binary);
    file.read(reinterpret_cast<char*>(&cars), sizeof(cars));
    file.read(reinterpret_cast<char*>(&students), sizeof(students));
    file.close();
}

void displayCarInfo(const SmartCar& car, const Student& student) {
    cout << "car.id: " << car.id << endl;
    cout << "car.chassis.id: " << car.chassis.id << endl;
    cout << "chassis.model: " << car.chassis.model << endl;
    cout << "studentId:  " << student.studentId << ", name " << student.name << endl;
}
